<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/27 11:18
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\common\model\admin;


use app\common\utils\Res;
use app\common\validate\RoleValidate;
use think\Model;

class RoleModel extends Model
{

    protected $name="sys_role";
    protected $pk="role_id";

    public static function add($param,$ruls)
    {
        try {
            $validate = new RoleValidate();
            if (!$validate->check($param)) {
                return Res::ModelMsg($validate->getError());
            }
            $roleModel = new RoleModel();
            $roleModel->save($param);
            $rolemenuModel = new RoleMenuModel();
            foreach ($ruls as $k=>$v){
                $data=[
                    'role_id'=>$roleModel->role_id,
                    'menu_id'=>$v
                ];

                $rolemenuModel->insert($data);
            }
            return  Res::Modelok("添加成功");
        }catch (\PDOException $exception){
            return Res::ModelMsg($exception->getMessage());
        }
    }


    public static function edit($param,$ruls)
    {
        try {
            $validate = new RoleValidate();
            if (!$validate->check($param)) {
                return Res::ModelMsg($validate->getError());
            }
            self::update($param, ['role_id' => $param['role_id']]);
            RoleMenuModel::delRoleId($param['role_id']);
            $rolemenuModel = new RoleMenuModel();

            foreach ($ruls as $k=>$v){
                $data=[
                    'role_id'=>$param['role_id'],
                    'menu_id'=>$v
                ];

                $rolemenuModel->insert($data);
            }
            return  Res::Modelok("修改成功");
        }catch (\PDOException $exception){
            return Res::ModelMsg($exception->getMessage());
        }
    }




    public static function pageMap($map,$offset,$limit){
        return self::where($map)->limit($offset, $limit)->order('role_id desc')->select();
    }

    public static function pageMapCount($map){
        return self::where($map)->count();
    }



    public static function del($id)
    {
        try{

             $res=self::find($id);
            if($id==1){
                return Res::ModelMsg('超级管理员不允许删除');
            }
            if(empty($res)){
                return Res::ModelMsg('你要删除的内容不存在');
            }
            self::destroy($id);
            RoleMenuModel::delRoleId($id);
            return Res::Modelok('删除成功');
        }catch (PDOException $e){
            return Res::ModelMsg($e->getMessage());
        }
    }


    public static function changeStatus($id)
    {
        try{
            $result = self::find($id);
            $status= $result['status']==0?1:0;
            self::where('role_id',$id)->update(['status'=>$status]);
            return Res::Modelok('修改成功');
        }catch (\PDOException $e){
            return Res::ModelMsg($e->getMessage());
        }

    }

    public static function changeOrder($param)
    {
        try{
            self::where('role_id',$param['id'])->update(['order_num'=>$param['num']]);
            return Res::Modelok('修改成功');
        }catch (\PDOException $e){
            return Res::ModelMsg($e->getMessage());
        }
    }

    /**
     * @param $param 接受要删除的ID数组
     * @return array|mixed
     */
    public static function batchRemove($param)
    {
        try {
            if(in_array(1,$param)){
                return Res::ModelMsg("超级管理员不允许删除");
            }
            foreach ($param as $k=>$v){
                RoleMenuModel::delRoleId($v);
            }
            self::destroy($param);
            return Res::Modelok( '删除成功');
        }catch(PDOException $e){
            return Res::ModelMsg( $e->getMessage());
        }
    }


    public static function findRoleAll()
    {
        return self::where('status',0)->select();
    }
}